// JavaScript Document
//@require Class.js

var Velocimetro = new MyClass({
	/**  
     * Property: velocidad
     * {Integer} values: 0 to 200
     */
	velocidad: 0, 
	/**  
     * Property: estado
     * {Integer} values: 0 to 2 - (0=apagado, 1:encendido, 2:desconocido)
     */
	estado: 2,
	/**  
     * Property: imagesURL
     * {String}
     */
	imagesURL: 'js/tablero/images/', 
	/** 
     * Property: display
     * {DOMElement} 
     */
	display: null, 
	/** 
     * SIZE of DISPLAY
     */
	width: '185px',
	height: '150px',
	/** 
     * Property: aguja
     * {DOMElement: div} 
     */
	aguja: null, 
	agujaWidth: '7585px',
	/** 
     * Property: startLed
     * {DOMElement: div} 
     */
	startLed: null,
	/** 
     * Property: maxima
     * {Integer} 
     */
	maxima: 200,
	/** 
     * Property: animationTimeOut
     * {Integer} 
     */
	animationTimeOut: null,
	/** 
     * Property: active
     * {boolean} 
     */
	active: true,

	init: function(imagesURL){
		if (imagesURL!=null)
			this.imagesURL = imagesURL;
		this.velocidad = 0;
		this.display = document.createElement("div");
		with (this.display.style){
			width = this.width;
			height = this.height;
			background = "transparent url("+this.imagesURL+"velocimetro.gif) no-repeat center";
		}
		this.aguja = document.createElement("div");
		with (this.aguja.style){
			width = this.width;
			height = this.height;
			background = "transparent url("+this.imagesURL+"agujaVelocimetro.gif) no-repeat";			
		}
		this.startLed = document.createElement("div");
		with (this.startLed.style){
			width = this.width;
			height = this.height;
			background = "transparent url("+this.imagesURL+"velocimetroLedOff.gif) no-repeat";			
		}
		this.aguja.appendChild(this.startLed);
		this.display.appendChild(this.aguja);
	},
	
	/** 
     * Method: putDisplayIn
     *  
     * Parameters:
     * idElement - {String} 
     */
	putDisplayIn: function(idElement){
		document.getElementById(idElement).appendChild(this.display);
	},
	
	/** 
     * Method: activate
     *  Activa/desactiva el medidor de velocidad
     * Parameters:
     * 	[state] - {Boolean} 
     */
	activate: function(state) {
		if ((state!=null)&&(state==false)){
			if (this.active){
				this.active = false;
				this.aguja.style.background = "";
				this.startLed.style.background = "transparent url("+this.imagesURL+"velocimetroLedOff.gif) no-repeat";
				this.velocidad = 0;
			}
		}
		else{
			if (!this.active){
				this.active = true;
				this.aguja.style.background = "transparent url("+this.imagesURL+"agujaVelocimetro.gif) no-repeat";
			}
		}		
	},
	
	/** 
     * Method: setValue
     *  
     * Parameters:
     * value - {Integer} 
     */
	setValue: function(value) {
		//activate/deactivate----------------------
		if ((value==null)||(isNaN(value)))
			this.activate(false);
		else	
			this.activate(true);
		if (!this.active) return;
		//------------------------------------------
		value = parseInt(value);
		if (value > this.maxima)
			value = this.maxima;
		else
			if (value < 0)
				value = 0;
		var agujaFrames = Math.floor(parseInt(this.agujaWidth)/parseInt(this.width))-1;
		var val=0;
		if (agujaFrames>1){
			var interval = this.maxima/agujaFrames;
			var val = Math.round(value/interval);
		}
		var offsetX = (-1) * val * parseInt(this.width);
		var backgroundPosition = offsetX+'px 0px';		
		this.aguja.style.backgroundPosition = backgroundPosition;
		this.velocidad = value;
		this.generateTooltip();
	},		
	
	/** 
     * Method: moveToValue
     *  
     * Parameters:
     * value - {Integer} 
     */
	moveToValue: function(toValue) {
		//activate/deactivate----------------------
		if ((toValue==null)||(isNaN(toValue)))
			this.activate(false);
		else	
			this.activate(true);
		if (!this.active) {
			this.generateTooltip();
			return;
		}
		//------------------------------------------
		if(this.animationTimeOut!=null)
			clearTimeout(this.animationTimeOut);
		toValue = parseInt(toValue);
		if (toValue > this.maxima)
			toValue = this.maxima;
		else
			if (toValue < 0)
				toValue = 0;
		var fromValue = this.velocidad;
		var inc = true;
		if (fromValue>toValue)
			inc = false;
		var value = fromValue;
		var me = this;
		var f=function animated(){
			(inc)?value++:value--;
			me.setValue(value);						
			if (value!=toValue){
				me.animationTimeOut=setTimeout(f,5);
			}
		}
		if (toValue!=value)
			this.animationTimeOut=setTimeout(f,5);
	},
	
	/** 
     * Method: setState
     *  
     * Parameters:
     * 	value - {Integer} : 0=apagado, 1:encendido, 2:desconocido
     */
	setState: function(value) {
		value = (((value==null)||(isNaN(value)))?2:parseInt(value));
		if (this.estado!=value){
			this.estado = value;
			switch(value){
				case 0: this.startLed.style.background = "transparent url("+this.imagesURL+"velocimetroLedYellow.gif) no-repeat"; break;
				case 1: this.startLed.style.background = "transparent url("+this.imagesURL+"velocimetroLedGreen.gif) no-repeat"; break;
				case 2: this.startLed.style.background = "transparent url("+this.imagesURL+"velocimetroLedOff.gif) no-repeat"; break;
				default: this.startLed.style.background = "transparent url("+this.imagesURL+"velocimetroLedOff.gif) no-repeat"; 
			}
		}
	},
	
	/** 
     * Method: generateTooltip
	 *   Formatea e inserta el tooltip (title) al componente actual
     * Parameters:
     * 	toValue - {Integer} 
     */
	generateTooltip: function(){
		var texto = 'VELOCIMETRO: \n ';		
		if (this.active){
			texto += ' Velocidad = ';
			texto += this.velocidad + ' km/h. \n';
			texto += ' Contacto de motor = ';
			switch (this.estado){
				case 0: texto += 'Apagado'; break;
				case 1: texto += 'Encendido'; break;
				case 2: texto += 'Desconocido'; break;
				default: texto += 'Desconocido';
			}
		}
		else
			texto += ' desactivado';
		this.display.title = texto;
	}
	
});